#!/usr/bin/env python
import pymongo

def connect_db(host='localhost',port=27017,db='test',user=None,passwd=None):
    connetion=pymongo.Connection(host=host,port=int(port))
    db=connetion[db]
    if user!=None and passwd!=None:
        db.authenticate(name=user,password=passwd)
    return db

def find_items(db,table,condition=None,extcdt=None):
    collection=db[table]
    items=collection.find(condition,extcdt)
    return items

def count_items(db,table,condition=None,extcdt=None):
    collection=db[table]
    cnt=collection.find(condition,extcdt).count()
    return cnt

def insert_item(db,table,item):
    collection=db[table]
    collection.insert(item)
    
def remove_items(db,table,condition=None):
    collection=db[table]
    collection.remove(condition)
    
def drop_collection(db,table):
    collection=db[table]
    collection.drop();

def export_item(items,file):
    f=open(file,'w')
    for item in items:
        f.write(str(item)+"\n")
        
